package edu.ncst.baseweb.service.impl.borrow;

import cn.wintersun.basecommon.common.Result;
import edu.ncst.baseweb.dao.BorrowRecord;
import edu.ncst.baseweb.dao.User;
import edu.ncst.baseweb.dao.equ.Equ;
import edu.ncst.baseweb.mapper.BorrowRecordMapper;
import edu.ncst.baseweb.mapper.EquClassMapper;
import edu.ncst.baseweb.mapper.EquMapper;
import edu.ncst.baseweb.mapper.UserMapper;
import edu.ncst.baseweb.service.intrer.borrow.ManagementRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ManagementRecordImpl implements ManagementRecordService {
    @Autowired
    private BorrowRecordMapper borrowRecordMapper;
    @Autowired
    private EquMapper equMapper;
    @Autowired
    private EquClassMapper equClassMapper;
    @Autowired
    private UserMapper userMapper;


    public Result<IPage<BorrowRecord>> list(BorrowRecord borrowrecord) {
        QueryWrapper<BorrowRecord> queryWrapper = Wrappers.query();

        //获取传回来的数据的设备名称
        Equ equ1 = borrowRecordMapper.getEquByEquId(borrowrecord.getEquId());
        //用名称和数据库中设备名称匹配
        if (StringUtils.isNotEmpty(borrowrecord.getEquName())) {
            queryWrapper.like(equ1.getName(), borrowrecord.getEquName());
        }

        Page<BorrowRecord> userPage = new Page<>(borrowrecord.getCurrent(), borrowrecord.getPageSize());
        IPage<BorrowRecord> borrowRecordPage = borrowRecordMapper.selectPage(userPage, queryWrapper);

        //将设备表中的相关数据拿到
        List<BorrowRecord> records = borrowRecordPage.getRecords();
        for (BorrowRecord record : records) {
            Equ equ = borrowRecordMapper.getEquByEquId(record.getEquId());
            User user = borrowRecordMapper.getUserByUserId(record.getUserId());

            record.setUserName(user.getUserName());
            record.setUserNumber(user.getNumber());

            record.setEquCode(equ.getEquCode());
            record.setEquName(equ.getName());


        }
        return Result.ok(borrowRecordPage);
    }
}
